/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hddz.comlib.ui.base.quicksearchbox;

import android.os.Handler;

import com.hddz.comlib.ui.base.quicksearchbox.util.Consumer;
import com.hddz.comlib.ui.base.quicksearchbox.util.NamedTask;
import com.hddz.comlib.ui.base.quicksearchbox.util.NamedTaskExecutor;

/**
 * A task that gets suggestions from a corpus.
 */
public class QueryTask<C extends SuggestionCursor> implements NamedTask
{
	private final String						mQuery;
	private final int							mQueryLimit;
	private final SuggestionCursorProvider<C>	mProvider;
	private final Handler						mHandler;
	private final Consumer<C>					mConsumer;

	/**
	 * Creates a new query task.
	 * 
	 * @param query
	 *            Query to run.
	 * @param queryLimit
	 *            The number of suggestions to ask each provider for.
	 * @param provider
	 *            The provider to ask for suggestions.
	 * @param handler
	 *            Handler that {@link Consumer#consume} will get called on. If
	 *            null, the method is called on the query thread.
	 * @param consumer
	 *            Consumer to notify when the suggestions have been returned.
	 */
	public QueryTask(String query, int queryLimit,
			SuggestionCursorProvider<C> provider, Handler handler,
			Consumer<C> consumer)
	{
		mQuery = query;
		mQueryLimit = queryLimit;
		mProvider = provider;
		mHandler = handler;
		mConsumer = consumer;
	}

	public String getName()
	{
		return mProvider.getName();
	}

	public void run()
	{
		final C cursor = mProvider.getSuggestions(mQuery, mQueryLimit);
		if (mHandler == null)
		{
			mConsumer.consume(cursor);
		}
		else
		{
			mHandler.post(new Runnable()
			{
				public void run()
				{
					boolean accepted = mConsumer.consume(cursor);
					if (!accepted)
					{
						cursor.close();
					}
				}
			});
		}
	}

	@Override
	public String toString()
	{
		return mProvider + "[" + mQuery + "]";
	}

	public static <C extends SuggestionCursor> void startQueries(String query,
			int maxResultsPerProvider,
			Iterable<? extends SuggestionCursorProvider<C>> providers,
			NamedTaskExecutor executor, Handler handler, Consumer<C> consumer)
	{
		for (SuggestionCursorProvider<C> provider : providers)
		{
			QueryTask<C> task = new QueryTask<C>(query, maxResultsPerProvider,
					provider, handler, consumer);
			executor.execute(task);
		}
	}
}
